﻿Partial Class WebForm_ManageHonorCert
    Inherits My.MyPage

    Protected Sub SearchHonorCert1_Loaded(ByVal source As Object, ByVal e As System.EventArgs) Handles SearchHonorCert1.Loaded
        '      Me.SearchHonorCert1.BindGV()
    End Sub

    Protected Sub QueryHonorCert1_Loaded(ByVal source As Object, ByVal e As System.EventArgs) Handles QueryHonorCert1.Loaded
        'Me.QueryHonorCert1.BindGV()
    End Sub

    Protected Sub SearchHonorCert1_SelectedIndexChanged(ByVal source As Object, ByVal e As System.EventArgs) Handles SearchHonorCert1.SelectedIndexChanged

        Me.TabContainer1.ActiveTab = Me.AddTabPanel
        Me.AddHonorCert1.InitDataForInsert(Me.SearchHonorCert1.ApplicationID)
        Me.AddHonorCert1.DataAccessMode = DataAccessMode.Insert
    End Sub

    Protected Sub QueryHonorCert1_SelectedIndexChanged(ByVal source As Object, ByVal e As System.EventArgs) Handles QueryHonorCert1.SelectedIndexChanged
        Me.TabContainer1.ActiveTab = Me.AddTabPanel
        Me.AddHonorCert1.InitDataForEdit(Me.QueryHonorCert1.SelectedHonorCertID)
        Me.AddHonorCert1.DataAccessMode = My.DataAccessMode.Update
    End Sub

    Protected Sub btnQueryClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles QueryHonorCert1.btnQueryClick
        Me.QueryHonorCert1.BindGV()
    End Sub

    Protected Sub AddHonorCert1_Saving(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddHonorCert1.Saving
        If Me.SaveData = True Then
            Me.SearchHonorCert1.BindGV()
            Me.QueryHonorCert1.BindGV()
            Me.TabContainer1.ActiveTab = Me.QueryTabPanel
            MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData_Success"))
            Me.AddHonorCert1.Visible = False
        Else
            Me.AddHonorCert1.Visible = True
        End If
    End Sub

    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)

    End Sub

    Protected Sub SearchAllHonorCert1_SelectedIndexChanged(ByVal source As Object, ByVal e As System.EventArgs) Handles SearchAllHonorCert1.SelectedIndexChanged

        Me.TabContainer1.ActiveTab = Me.AddTabPanel
        Me.AddHonorCert1.InsertWorkHour(Me.SearchAllHonorCert1.VolunteerID, Me.SearchAllHonorCert1.VolunteerName)
        Me.AddHonorCert1.DataAccessMode = My.DataAccessMode.Insert
        Me.AddHonorCert1.Visible = True
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Me.CanAdd = False Then
            Me.AddTabPanel.Visible = False
            ' Me.SearchAllTabPanel1.Visible = False
            Me.SearchHonorCert1.IsVisible = False
            Me.SearchAllHonorCert1.IsVisible = False
        End If
    End Sub
End Class

Partial Class WebForm_ManageHonorCert

    Private Function SaveData() As Boolean
        Dim ErrorMessage As String = ""
    
        If Me.HonorCertCodeIsExist(Me.AddHonorCert1.HonorCertCode) = True Then
            ErrorMessage += "此榮譽卡號碼已存在,"

            Me.AddHonorCert1.HonorCertCode = ""
        End If
        '申請日期ApplyDate<=核發日期IssueDate<有效期限EndDate
        If Me.AddHonorCert1.IssueDate < Me.AddHonorCert1.ApplyDate Then
            ErrorMessage += "核發日期必須大於等於申請日期,"
        End If

        If Me.AddHonorCert1.EndDate <= Me.AddHonorCert1.IssueDate Then
            ErrorMessage += "有效期限必須大於核發日期,"
        End If

        If Me.AddHonorCert1.EndDate <= Me.AddHonorCert1.ApplyDate Then
            ErrorMessage += "有效期限必須大於申請日期,"

        End If
        If ErrorMessage <> "" Then
            MyObj.ShowMessage(String.Format("{0}", ErrorMessage.TrimEnd(",")))
            Return False
        End If

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope
            Select Case Me.AddHonorCert1.DataAccessMode
                Case My.DataAccessMode.Insert

                    Dim FinalWorkHour As Double
                    Dim HornorApplication As New Data.DataTable
                
                    If Me.AddHonorCert1.ApplicationID Is Nothing Then
                        If Me.AddHonorCert1.WorkHour = "" OrElse IsNumeric(Me.AddHonorCert1.WorkHour) = False OrElse Int32.Parse(Me.AddHonorCert1.WorkHour) < 0 Then
                            MyObj.ShowMessage("其他領域時數必須是正整數且不可空白!")
                            Return False
                        End If
                        Dim WorkHour As String = ""
                        sql.Length = 0
                        sql.AppendFormat("Select  sum(WorkHour) as WorkHour from MonthlyVolunteerStat ")
                        sql.AppendFormat("where VolunteerID='{0}' ", Me.AddHonorCert1.VolunteerID)
                        sql.AppendFormat("group by VolunteerID ")
                        WorkHour = dat.GetSingleData(sql.ToString(), Nothing, Data.CommandType.Text)

                        FinalWorkHour = Double.Parse(Me.AddHonorCert1.WorkHour) + Double.Parse(WorkHour)

                        Dim ApplyHour As Double = Double.Parse(MyObj.GetParameter("HonorCert", "ApplyWorkHour"))

                        If FinalWorkHour >= ApplyHour Then
                            Dim yymmdt As Data.DataTable

                            sql.Length = 0
                            sql.AppendFormat("select top 1 YYMM from MonthlyVolunteerStat ")
                            sql.AppendFormat("where VolunteerID='{0}' ", Me.AddHonorCert1.VolunteerID)
                            yymmdt = dat.GetData(sql)
                            sql.Length = 0
                            sql.AppendFormat("Select ApplicationID from HonorCertApplication ")
                            sql.AppendFormat("where Status='WA' ")
                            sql.AppendFormat("and VolunteerID='{0}' ", Me.AddHonorCert1.VolunteerID)
                            HornorApplication = dat.GetData(sql)
                            sql.Length = 0
                            sql.AppendFormat("Update MonthlyVolunteerStat Set ")
                            sql.AppendFormat("WorkHour ={0} ", FinalWorkHour)
                            sql.AppendFormat("where VolunteerID='{0}' ", Me.AddHonorCert1.VolunteerID)
                            sql.AppendFormat("and YYMM ='{0}'; ", yymmdt.Rows(0)("YYMM").ToString())

                           
                        Else

                            MyObj.ShowMessage("您的總服務時數未達{0}小時，發放榮譽卡失敗！", ApplyHour)
                            Return False
                        End If
                    End If


                    sql.AppendFormat("insert HonorCert (VolunteerID,HonorCertCode,EndDate,IssueDate,CreateDate,CreateBy,Active,ApplyDate,Comment,Type) select ")
                    sql.AppendFormat("'{0}' ", Me.AddHonorCert1.VolunteerID)
                    sql.AppendFormat(",'{0}' ", Me.AddHonorCert1.HonorCertCode)
                    sql.AppendFormat(",'{0:yyyy/MM/dd}' ", Me.AddHonorCert1.EndDate)
                    sql.AppendFormat(",'{0:yyyy/MM/dd}' ", Me.AddHonorCert1.IssueDate)
                    sql.AppendFormat(",{0} ", "getdate()")
                    sql.AppendFormat(",'{0}' ", Me.LoginID)
                    sql.AppendFormat(",'{0}' ", "Y")
                    sql.AppendFormat(",'{0:yyyy/MM/dd}' ", Me.AddHonorCert1.ApplyDate)
                    sql.AppendFormat(",'{0}' ", Me.AddHonorCert1.Comment)
                    sql.AppendFormat(",'{0}'; ", Me.AddHonorCert1.Type)


                    If Not Me.AddHonorCert1.ApplicationID Is Nothing Then

                        sql.AppendFormat("update HonorCertApplication set Status='AP' ")
                        sql.AppendFormat(",WorkHour= vs.HornorHour from (Select  HornorHour from VolunteerStatv2  ")
                        sql.AppendFormat("where VolunteerID='{0}') vs ", Me.AddHonorCert1.VolunteerID)
                        sql.AppendFormat(" where ApplicationID={0}; ", Me.AddHonorCert1.ApplicationID)
                    Else
                        If HornorApplication.Rows.Count = 0 Then
                            sql.AppendFormat("Insert Into HonorCertApplication (VolunteerID,CreateDate,CreateBy,Status,WorkHour) ")
                            sql.AppendFormat("Select '{0}' ", Me.AddHonorCert1.VolunteerID)
                            sql.AppendFormat(",getdate() ")
                            sql.AppendFormat(",'{0}' ", clsAuth.GetUserID())
                            sql.AppendFormat(",'{0}' ", "AP")
                            sql.AppendFormat(",'{0}'; ", FinalWorkHour)
                        Else
                            sql.AppendFormat("Update HonorCertApplication Set ")
                            sql.AppendFormat("Status='AP' ")
                            sql.AppendFormat(",WorkHour= vs.HornorHour from (Select  HornorHour from VolunteerStatv2  ")
                            sql.AppendFormat("where VolunteerID='{0}') vs ", Me.AddHonorCert1.VolunteerID)
                            sql.AppendFormat(" where VolunteerID='{0}' AND Status='WA'; ", Me.AddHonorCert1.VolunteerID)
                        End If

                        End If

                        Try
                            dat.AccData(sql)
                        Catch ex As Exception
                            MyObj.ShowMessage("發放失敗!")
                            MyObj.WriteErrorLog(sql.ToString())
                        End Try




                Case My.DataAccessMode.Update
                        sql.Length = 0
                        sql.AppendFormat("update HonorCert set ")
                        sql.AppendFormat(" HonorCertCode='{0}' ", Me.AddHonorCert1.HonorCertCode)
                        sql.AppendFormat(" ,EndDate='{0:yyyy/MM/dd}' ", Me.AddHonorCert1.EndDate)
                        sql.AppendFormat(" ,ApplyDate='{0:yyyy/MM/dd}' ", Me.AddHonorCert1.ApplyDate)
                        sql.AppendFormat(" ,IssueDate='{0:yyyy/MM/dd}' ", Me.AddHonorCert1.IssueDate)
                        sql.AppendFormat(",UpdateDate={0} ", "getdate()")
                        sql.AppendFormat(",UpdateBy='{0}' ", Me.LoginID)
                    sql.AppendFormat(",Comment='{0}' ", Me.AddHonorCert1.Comment)
                    sql.AppendFormat(",Type='{0}' ", Me.AddHonorCert1.Type)
                        sql.AppendFormat(" where HonorCertID='{0}' ", Me.QueryHonorCert1.SelectedHonorCertID)
                        dat.AccData(sql)

            End Select
            scope.Complete()
        End Using

        Return True
    End Function
    '判斷榮譽卡號碼是否存在 
    Private Function HonorCertCodeIsExist(ByVal Code As String) As Boolean
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim dt As New Data.DataTable
        sql.AppendFormat("Select VolunteerID,HonorCertCode From HonorCert ")
        sql.AppendFormat("Where HonorCertCode='{0}' ", Code)
        dt = dat.GetData(sql)
        If dt.Rows.Count >= 1 Then
            '如果是原帳號的榮譽卡,則可以儲存
            For Each dr As Data.DataRow In dt.Rows
                If dr("VolunteerID") = Me.AddHonorCert1.VolunteerID Then
                    Return False
                    Exit For
                End If
            Next
            Return True
        Else
            Return False
        End If
    End Function
    Private ReadOnly Property LoginID() As String
        Get
            Return clsAuth.GetUserID()
        End Get
    End Property
End Class

